package com.hegongda.dao.impl;

import com.hegongda.bean.User;
import com.hegongda.dao.UserDao;
import com.hegongda.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;


import java.sql.SQLException;


public class UserDaoImpl implements UserDao {
    public int userRegister(User user) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String str = "insert user values (?,?,?,?,?,?,?,?,?,?)";
        Object[] object = {user.getUid(),user.getUsername(),user.getPassword(),user.getName(),user.getEmail(),user.getBirthday(),user.getGender(),user.getState(),user.getCode(),user.getRemark()};
        int update = qr.update(str, object);
        return update;
    }

    public User userLogin(String name,String password) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "select * from user where username = ? and password = ? and state = 1";
        Object[] object = {name,password};
        User query = qr.query(sql, new BeanHandler<User>(User.class), object);
        return query;
    }
    public User hasUser(String name) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "select * from user where username = ?";
        Object[] object = {name};
        User query = qr.query(sql, new BeanHandler<User>(User.class), object);
        return query;
    }
}
